package com.cms.controller;
 
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;
import com.cms.model.TrLotteryVo;
import com.cms.service.LotteryService;
import com.cms.service.ReportService;
import com.google.gson.Gson;

@Controller
public class LotteryController extends BaseController {

	 @Autowired
	 private LotteryService lotteryService;
	 
	 @Autowired
	 private ReportService reportService;
	 
	 @RequestMapping("/lotteryService.do")
	 public String enter(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 Map<String,Object>param = new HashMap<String, Object>();
		 param.put(CmsConstant.CATALOG_NAME, "LOTTERY_TYPE");
		 map.put("lotteryTypes", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
		 return "lottery/lotteryMain.html";
	 }
	 
	 
	 @RequestMapping("/lotteryService/queryLotteryPlayGrooup.do")
	 public void queryLotteryPlayGrooup(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 { 
		 this.renderText(resp, new Gson().toJson(this.reportService.getReportResult("queryLotteryPlayGroup", this.buildQueryMap(req))));
	 }
	 
	 @RequestMapping("/lotteryService/pagingLotteryList.do")
	 public String pagingLotteryList(HttpServletRequest request,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 map.put("lotteryList", this.reportService.pagingReportResult("paginglotteryList",this.buildQueryMap(request)));
		 return "lottery/lotteryList.html";
	 }
	 
	 
	 @RequestMapping("/lotteryService/toEditLottery.do")
	 public String toEditLottery(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 int lottery = StringUtils.str2Int(req.getParameter("lotteryId"));
		 TrLotteryVo vo = new TrLotteryVo();
		 vo.setId(lottery);
		 map.put("lottery", this.lotteryService.getLotteryInfo(vo));
		 Map<String,Object>param = new HashMap<String, Object>();
		 param.put(CmsConstant.CATALOG_NAME, "LOTTERY_TYPE");
		 map.put("lotteryTypes", this.reportService.getReportResult(CmsConstant.CATALOG_CODE, param));
		 return "lottery/lotteryInfo.html";
	 }
	 
	 
	 @RequestMapping("/lotteryService/updateLottery.do")
	 public void updateLottery(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
			TrLotteryVo account = new TrLotteryVo();
			TrAdminUser tu = this.getLoginUser(req);
			account.setCreateUser(tu.getId());
			account.setModifyUser(tu.getId());
			Map<String, Object> param = new HashMap<String, Object>();
			try {
				this.export(req, "lottery", account);
				this.lotteryService.updateLottery(account);
				param.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
			} catch (Exception e) {
				param.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				param.put(CmsConstant.MSG, e.getMessage());
			}
			this.renderText(resp, new Gson().toJson(param));
	 }
}
